﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Speech.Synthesis;
using luck_roll.Config;
using System.Media;
using System.ComponentModel;
using NAudio.Wave;

namespace luck_roll.Common
{
    /// <summary>
    /// 播放工具帮助类
    /// </summary>
    public class SoundPlayerHelper
    {
        private string filePath = string.Empty;

        //private SoundPlayer soundPlayer = null;

        private bool isStart = false;


        private IWavePlayer waveOut;
        private AudioFileReader audioFileReader;
        private WaveStream waveStream;
        public SoundPlayerHelper()
        {
            string currentPath = Environment.CurrentDirectory;
            string folderPath = Path.Combine(currentPath, "resources");
            filePath = Path.Combine(folderPath, ConfigHelper.currentAppConfig.BackgroundMusic);
        }

        public void Start()
        {

            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                isStart=true;
                BackgroundWorker bw=new BackgroundWorker();
                bw.DoWork += (sender, e) => {
                    waveOut = new WaveOutEvent();
                    waveStream = new BlockAlignReductionStream(new AudioFileReader(filePath));
                   
                    waveOut.Init(waveStream);
                    waveOut.Play();

                };

                if(!bw.IsBusy)
                {
                    bw.RunWorkerAsync();
                }
              
            }
        }


        public void Stop()
        {
            isStart = false;
            if (waveOut != null)
            {
                waveOut.Stop();
                waveOut.Dispose();
            }
        }

    }
}
